package model

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"quiz-master/appconfig"
)

var DB *gorm.DB
var err error

func Init() {
	MysqlApp := appconfig.Config.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		MysqlApp.User,
		MysqlApp.Password,
		MysqlApp.Host,
		MysqlApp.Port,
		MysqlApp.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库连接成功")
	err = DB.AutoMigrate(
		&User{},
		&UserRole{},
		&Role{},
		&RoleNode{},
		&Node{},
		///////仓库订单需求////////////
		&Repo{},
		&RepOrder{},
		//////货物车辆配送订单///////
		&Car{},
		&CarOrder{},
		/////////////商品订单////////
		&Good{},
		&GoodOrder{},
		//////配送订单//
		&PerSon{},
		&PserSonOrder{},
		//////车辆交易订单////////
		&BigCar{},
		&BigCarOrder{},
	)
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库迁移成功")
}
